package nettydemo;/*

 */

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @ClassName NIOClient
 * @Author Final
 * @Project DataStructures
 */
public class NIOClient {
    public static void main(String[] args) throws IOException {
        //得到一个网络通道
        SocketChannel serverSocketChannel = SocketChannel.open();

        //设置非阻塞
        serverSocketChannel.configureBlocking(false);

        //提供服务端的ip和端口
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 801);

        //连接服务器
        if (!serverSocketChannel.connect(inetSocketAddress)){
            while (!serverSocketChannel.finishConnect()){
                System.out.println("因为连接需要事件，客户端不会阻塞，可以做其他工作");

            }
        }

        String str="hello";

        ByteBuffer wrap = ByteBuffer.wrap(str.getBytes());

        //发送数据
        //将buffer数据写入channel
        serverSocketChannel.write(wrap);

        System.in.read();
    }
}
